<template>
    <div class="mainBox">
        <div class="formBox">
            <el-form :inline="true" :model="ruleForm" ref="ruleForm" label-width="110px" class="formBox">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="日期">
                            <el-date-picker unlink-panels v-model="ruleForm.data" @change="echartsTime" value-format="yyyy-MM-dd" type="daterange" range-separator="至" :start-placeholder="startTime" :end-placeholder="endTime">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item class="secondRow_formItem">
                            <el-button type="primary" icon="el-icon-search" @click="query">查询</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="tableBox">
            <div class="export">
                <el-button type="info" icon="el-icon-download" size="small" @click="exportExcel">导出</el-button>
            </div>
            <el-table show-summary :summary-method="getSummaries" :data="tableData" style="width:100%" stripe border id="out-table2" v-loading="tableLoading">
                <el-table-column type="index" label="序号" width="80" :index="dealIndex" center></el-table-column>
                <el-table-column prop="memberValue" label="会员姓名" center></el-table-column>
                <el-table-column prop="consumptionNumber" label="消费单号" center></el-table-column>
                <el-table-column prop="enterIntegral" label="入账积分" center></el-table-column>
                <el-table-column prop="consumptionMoney" label="入账金额" center></el-table-column>
                <el-table-column prop="storeValue" label="消费门店" center></el-table-column>
                <el-table-column prop="createTime" label="消费时间" center></el-table-column>
            </el-table>
            <el-pagination style="padding:20px 0;text-align:center;" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="current" :page-sizes="[2, 5, 10, 20, 50]" :page-size="size" layout="total,sizes,prev,pager,next,jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { integralEnterLogInList } from "@/api/user";
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
  data() {
    return {
      ruleForm: {
        data: ""
      },
      startTime: "",
      endTime: "",
      size: 10,
      current: 1,
      total: 0,
      tableData: [],
      tableLoading: false
    };
  },
  mounted() {
    this.getNowFormatDate();
    this.order();
  },
  methods: {
    //总计求和
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
        } else if (index === 3 || index === 4) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = "N/A";
          }
        } else {
          sums[index] = "--";
        }
      });
      return sums;
    },
    query() {
      this.order();
    },
    // 记录导出
    exportExcel() {
      /* 从表生成工作簿对象 */
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      /* 获取二进制字符串作为输出 */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        FileSaver.saveAs(
          //Blob 对象表示一个不可变、原始数据的类文件对象。
          //Blob 表示的不一定是JavaScript原生格式的数据。
          //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
          //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
          new Blob([wbout], { type: "application/octet-stream" }),
          //设置导出文件名称
          "sheetjs.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    order() {
      let data = {
        storeId: JSON.parse(this.$store.state.user.getStore).id,
        startTime: this.startTime + " " + "00:00:00",
        endTime: this.endTime + " " + "23:59:59",
         current: this.current,
        size: this.size,
      };
      integralEnterLogInList(data).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.tableLoading=false
          this.total=res.data.total
        }else{
            this.$message.error(res.msg)
        }
      });
    },
    echartsTime(val) {
      this.startTime = val[0];
      this.endTime = val[1];
    },
    getNowFormatDate() {
      var date = new Date();
      var seperator1 = "-";
      var year = date.getFullYear();
      var year1 = date.getFullYear() - 1;
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      this.startTime = year1 + seperator1 + month + seperator1 + strDate;
      this.endTime = year + seperator1 + month + seperator1 + strDate;
    },
    handleSizeChange(val) {
      this.size = val;
      this.order();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.order();
    },
      // 抵扣积分记录导出
    exportExcel() {
      /* 从表生成工作簿对象 */
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table2"));
      /* 获取二进制字符串作为输出 */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        FileSaver.saveAs(
          //Blob 对象表示一个不可变、原始数据的类文件对象。
          //Blob 表示的不一定是JavaScript原生格式的数据。
          //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
          //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
          new Blob([wbout], { type: "application/octet-stream" }),
          //设置导出文件名称
          "sheetjs.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    dealIndex(index) {
      var self = this;
      return index + 1 + (self.current - 1) * self.size;
    }
  }
};
</script>

 <style lang="scss" scoped>
.formBox {
  margin-top: 20px;
}
.tableBox {
  margin: 58px 20px 0 20px;
  .export {
    text-align: right;
    margin-bottom: 30px;
  }
}
</style>


</style>
